import datetime

t=int(input())
time0=datetime.datetime(1970,1,1,0,0,0)
for _ in range(t):
    a=input().split()

    time=datetime.datetime.strptime(a[0]+" "+a[1],"%Y-%m-%d %H:%M:%S")

    x=int(a[2])
    # 求出所给时间与纪元时间的间隔
    delta=time-time0
    # 求出间隔与x的取整，这一步是算纪元时间与所给时间之间有多少个x
    n=delta//datetime.timedelta(minutes=x)
    res=(time0+n*datetime.timedelta(minutes=x)).strftime("%Y-%m-%d %H:%M:%S")
    print(res)